package com.ht.medicaltreatment.activity.haocai

import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.viewModels
import androidx.core.view.forEach
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.bean.ShenQinigAPIBean
import com.ht.medicaltreatment.http.api.ShenQingSubmitAPI
import com.ht.medicaltreatment.viewmodel.ShenQingViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_haocaishenqing_layout.btnPositive
import kotlinx.android.synthetic.main.activity_shenqing_layout.etName
import kotlinx.android.synthetic.main.activity_shenqing_layout.llContent

/**
 * 申请耗材
 */
class ShenQingActivity:BaseActivity(), OnItemChildClickListener {

    override fun getLayoutId() = R.layout.activity_shenqing_layout

    private val model by viewModels<ShenQingViewModel>()
    private var projectId = ""

    override fun init() {
        projectId = intent.getStringExtra("projectId")?:""
        showProgress()
        model.getList(projectId){
            dismissProgress()
            it.forEachIndexed { index, bean ->
                val view = getView()
                val tvCode = view.findViewById<TextView>(R.id.tvCode)
                val tvName = view.findViewById<TextView>(R.id.tvName)
                val tvRongLiang = view.findViewById<TextView>(R.id.tvRongLiang)

                tvCode.text = "${index+1}"
                tvCode.setTag("${bean.id}")
                tvName.text = bean.name
                tvRongLiang.text = bean.specification
                llContent.addView(view)
            }
        }
        //提交
        btnPositive.setOnClickListener {
            showProgress()
            val list = arrayListOf<ShenQinigAPIBean>()
            llContent.forEach { view ->
                val etCount = view.findViewById<EditText>(R.id.etCount)
                val tvCode = view.findViewById<TextView>(R.id.tvCode)
                val count = etCount.text.toString()
                val id = tvCode.getTag() as String
                list += ShenQinigAPIBean(id,count)
            }
            val name = etName.text.toString()
            val api = ShenQingSubmitAPI(name,projectId,list)
            model.submit(api){
                dismissProgress()
                showToast("提交成功")
                finish()
            }
        }
    }

    fun getView():View{
        val view = LayoutInflater.from(this).inflate(R.layout.item_shenqing_layout,null,false)
        val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        params.topMargin = 20
        params.leftMargin = 10
        params.rightMargin = 10
        view.layoutParams = params
        return view
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

    }
}